import { createStore } from 'vuex'
// import store from '@/store'
// import bus from '@/utils/mitt'
// import {
//     ossUploadFile
//     // resumeCloudUpload,
//     // getPath4CloudBase64
// } from '@/utils/file'
// import utils from '@/utils/utilns'
export default createStore({
    state: {
        token: '',
        platform: 2, // window=2 mac=3
        homeTreeInfo: {},
        companyInfo: {},
        userInfo: {},
        uploadingList: [],
        isTpye: 0,
        positionList: [] //文档最近移动位置
    },
    mutations: {
        updatePageType(state, data) {
            state.token = data
        },
        setPlatform(state, data) {
            state.platform = data
        },
        getUserInfo(state, data) {
            state.userInfo = data
        },
        getCompanyInfo(state, data) {
            state.companyInfo = data
        },
        setHomeTreeInfo(state, data) {
            state.homeTreeInfo = data
        },
        updateUploadingList(state, list) {
            state.uploadingList = list
        },
        clearUploadingList(state) {
            state.uploadingList = []
        },
        getIsTpye(state, data) {
            state.isTpye = data
        },
        setUserInfoAvatar(state, data) {
            state.userInfo.avatar = data
        },
        setPositionList(state, data) {
            state.positionList = data
        },
        addPositionList(state, data) {
            state.positionList.unshift(data)
                //  数组去重
            let newArr = []
            let t = data.type == 0 ? 'spaceId' : 'documentId'
            newArr = state.positionList.filter(function(item) {
                return item[t] && newArr.includes(item[t]) ? '' : newArr.push(item[t])
            })
            state.positionList = newArr
            localStorage.setItem('positionList', JSON.stringify(state.positionList))
        },
        // 删除不存在的数据
        deletePosition(state, data) {
            state.positionList.splice(
                state.positionList.findIndex((item) => item.time === data.time),
                1
            )
            localStorage.setItem('positionList', JSON.stringify(state.positionList))
        }
    },
    getters: {
        getToken(state, getters) {
            //getter相当于计算属性 获取state的值
            return state.token
        }
    },
    actions: {
        clearAllUpload({ commit, state, dispatch }) {
            const tempList = state.uploadingList
            tempList.forEach((item) => {
                if (item.uploadClient) {
                    item.uploadClient.cancel()
                }
                item.status = 3
            })
            commit('updateUploadingList', tempList)
            commit('clearUploadingList')
        }
    },
    modules: {}
})